import asyncio
import datetime
import subprocess
from time import sleep

import requests
import sys
import aiohttp  # 使用 aiohttp 替换 requests
# from DrissionPage import ChromiumPage, ChromiumOptions
from MysqlUtils import MysqlUtils

URL = "http://localhost:8081"

# 定义一个Semaphore来控制并发数
concurrent_limit = 10
semaphore = asyncio.Semaphore(concurrent_limit)


# 时间戳
def convert_timestamp_to_datetime(timestamp):
    return datetime.datetime.fromtimestamp(int(timestamp))


async def getSearchUser():
    pass


async def getReplys(video_id, comment_id, user_id):
    print("来了", video_id)
    mu = MysqlUtils()
    offset = 0  # 已经获取的
    limit = 20  # 获取的数据量
    params = {
        "id": video_id,
        'comment_id': comment_id,
        "offset": offset,
        "limit": limit
    }
    i = 0
    while True:
        try:
            i += 1
            async with aiohttp.ClientSession() as session:
                async with session.get(URL + "/douyin/replys", params=params) as res:
                    response = await res.json()
                    tow_comments = response["data"]["comments"]
                    if not tow_comments:
                        break
                    for tow_comment in tow_comments:
                        signature = ""

                        tow_data = {
                            "name": tow_comment['user']['nickname'],
                            "cid": tow_comment['cid'],
                            "text": tow_comment['text'],
                            "sec_uid": tow_comment['user']['sec_uid'],
                            "signature": signature,
                            "create_time": convert_timestamp_to_datetime(tow_comment['create_time']),
                        }
                        print("tow_data-----", tow_data["text"])
                        if tow_data["text"] != "":
                            await asyncio.to_thread(mu.cur.execute,
                                                    "INSERT INTO pinglun_list (nickname, cid, text, sec_uid, signature, create_time,u_id) VALUES (%s, %s,%s, %s, %s, %s, %s)",
                                                    (tow_data['name'], tow_data['cid'], tow_data['text'],
                                                     tow_data['sec_uid'],
                                                     tow_data['signature'], tow_data['create_time'], user_id))
                            await asyncio.to_thread(mu.con.commit)
            offset += len(tow_comments)
            params["offset"] = offset
        except aiohttp.ClientError as e:
            print(f"HTTP error occurred: {e}")
            break
        except Exception as e:
            print(f"An error occurred in getReplys: {e}")
            break


# async def get

async def getComments(video_id, user_id):
    async with semaphore:
        print("当前视频连接", video_id)
        mu = MysqlUtils()
        offset = 0  # 已经获取的
        limit = 20  # 获取的数据量
        params = {
            "id": video_id,
            "offset": offset,
            "limit": limit
        }
        i = 0
        while True:
            i += 1
            async with aiohttp.ClientSession() as session:
                async with session.get(URL + "/douyin/comments", params=params) as res:
                    response = await res.json()
                    print(response)
                    one_comments = response["data"]["comments"]
                    if not one_comments:
                        break
                    for one_comment in one_comments:
                        signature = ""
                        one_data = {
                            "name": one_comment['user']['nickname'],
                            "cid": one_comment['cid'],
                            "text": one_comment['text'],
                            "sec_uid": one_comment['user']['sec_uid'],
                            "signature": signature,
                            "create_time": convert_timestamp_to_datetime(one_comment['create_time']),
                        }
                        # print("one_data=============", one_data["text"])
                        if one_data["text"] != "":
                            await asyncio.to_thread(mu.cur.execute,
                                                    "INSERT INTO pinglun_list (nickname, cid, text, sec_uid, signature, create_time,u_id) VALUES (%s, %s, %s,%s, %s, %s, %s)",
                                                    (one_data['name'], one_data['cid'], one_data['text'],
                                                     one_data['sec_uid'], one_data['signature'],
                                                     one_data['create_time'], user_id))
                            await asyncio.to_thread(mu.con.commit)
                            await getReplys(video_id, one_comment['cid'], user_id)
            offset += len(one_comments)
            params["offset"] = offset


def addDouYin(Cookie):
    params = {
        "id": "cheshi",
        "cookie": Cookie
    }
    res = requests.post(URL + "/douyin/add_account", json=params)
    print(res.text)


def expire_account():
    data = {
        "id": "test",
    }
    response = requests.post(URL + "/douyin/expire_account", json=data)
    return response


def startSearch(keywords):
    param = {
        "keyword": keywords
    }
    response = requests.get(URL + "/douyin/search", params=param).json()
    # self.assertEqual(response.status_code, 200)
    # self.assertEqual(response.json()['code'], 0)
    # self.assertGreater(len(response.json()['data']), 0)
    return response


def startUserSearch(keywords):
    # / douyin / user_search?keyword = 吴彦祖
    param = {
        "keyword": keywords
    }
    response = requests.get(URL + "/douyin/user_search", params=param).json()
    print(response)
    # self.assertEqual(response.status_code, 200)
    # self.assertEqual(response.json()['code'], 0)
    # self.assertGreater(len(response.json()['data']), 0)
    return response


async def saveUserLink(url, uid):
    # pass
    mu = MysqlUtils()
    # https: // www.douyin.com / user / MS4wLjABAAAAhleHOXpjLXt1twayftvmz4ev_7iKx - rLAMcxrXV9DAI
    # ;
    await asyncio.to_thread(mu.cur.execute,
                            "INSERT INTO  `pinglun_menu`( `url`, `is_search`, `u_id`) VALUES(%s,%s,%s)",
                            (url, 0, uid))
    await asyncio.to_thread(mu.con.commit)



async def main():
    processed_videos = set()  # 用于存储已经处理过的视频ID
    tasks = []
    # 1.首先判断账户状态
    # 2. 添加账号
    # addDouYin("ttwid=1%7CGNLPNZfcjsinx7mzMsu_L9zIxAmMsDlagtrguMKBQoM%7C1742335319%7C8c898a9e8736979c6744d0f56ff7e143dc2f9317b10b933888928984fbccf10c; UIFID_TEMP=2eb4f745f9fe6544447c1d68cb43a44931f67e23b1951fd9ca8b76ce94a622362204e3bb605c35a27b0204811add7cf4800112128a38004b25a13a74f132385b66a531406c9946f5174ce0a50c364fd7; x-web-secsdk-uid=2dc94050-c743-49b3-b0fe-82fb5ceb27e6; s_v_web_id=verify_m8f1grdl_6rym1krR_GsA7_4Goc_BeuB_BHkrx9XErcNp; douyin.com; device_web_cpu_core=8; device_web_memory_size=8; architecture=amd64; dy_swidth=1440; dy_sheight=960; hevc_supported=true; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0.5%7D; passport_csrf_token=849103cfb373971d6cf581da1736463f; passport_csrf_token_default=849103cfb373971d6cf581da1736463f; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%7D; __security_mc_1_s_sdk_crypt_sdk=ae9c3723-47fd-bba0; __security_mc_1_s_sdk_cert_key=eda24a7d-4754-a985; bd_ticket_guard_client_web_domain=2; fpk1=U2FsdGVkX18wYCoFLR8zt2bVcW7FeeH4R7pLGQG0Fzjsl4PabRUBGT7uzL+0hUKlT+My7N7hugknxE+u5YhKtQ==; fpk2=3fa31b52dd6ebc517e5492d43d77e61c; is_dash_user=1; WallpaperGuide=%7B%22showTime%22%3A1742335888640%2C%22closeTime%22%3A0%2C%22showCount%22%3A1%2C%22cursor1%22%3A0%2C%22cursor2%22%3A0%7D; SEARCH_RESULT_LIST_TYPE=%22single%22; csrf_session_id=183b2bea3cb3300cad5fc2a4246ee8ab; UIFID=2eb4f745f9fe6544447c1d68cb43a44931f67e23b1951fd9ca8b76ce94a6223665d856ea2fbda0faa6e62d07f9d3203f2bcdc3bfc6350425ed699def1942ab003dbe48340ce033057e01ba0450d258ecd8d46ecbbbaa7f3cd61c40c5f873eaaf6645f590c266f27712e5243f1386b78df2b982e55ffd008dc71d5e864babcd494ca77e7a133265bc5ff09cf0df44a484842bc186cf332381f3ed14589d19893e125d581c369baf46812aa369ee0f7f8cbb4e61c67c4a6f560eca4df9ada7cc53; strategyABtestKey=%221742467629.697%22; xgplayer_user_id=921158892398; download_guide=%223%2F20250320%2F0%22; __ac_signature=_02B4Z6wo00f015bigZwAAIDCSbly0v-ibV-WwoUAAIJrd0; biz_trace_id=a46bbb27; passport_mfa_token=CjVyuCY5ml7YnYQzEaKWOlV3Rx4Px4TwP9KJgr9VtYS55K81kVYhGfc%2FIhKGEiLIu2jHBLTj0hpKCjyYTxnPKYYtCGPKS4X%2FHp2x0NR0%2BYYZNc2L7epHNEVydVCuqkwpgc5GIZ6Xe5kCd7adbYLJig4Hqkae4DAQq8LsDRj2sdFsIAIiAQMKhW5y; d_ticket=b15b0cd81983de53510bfb367b1a6fcd82b5e; passport_assist_user=Cj1FL3lUzsNhJILvn9vgxbPXls0N99M5xDgmXI6C2Ls9cCZLC4Wo9GQEuko98KDCpt0LtmgxwL6GCaqg3B4XGkoKPL-flLhdT27KdaTMC9aC30QFIwTEnGdm6Y0q9ZJLpfstSWgnGKFnRrQXVuMgcjEnHvs4hcq4ZR8DaMkfkRCqwuwNGImv1lQgASIBA2whxDk%3D; n_mh=qjOAQeV93S33wgG3xJYNkhdPNzPPo1QY5Q2AKHkTRZ4; passport_auth_status=79061b44125d99a3e7c223997237e2b5%2C; passport_auth_status_ss=79061b44125d99a3e7c223997237e2b5%2C; sid_guard=865311383398fa9effd331fcb2312d74%7C1742470223%7C5184000%7CMon%2C+19-May-2025+11%3A30%3A23+GMT; uid_tt=2803d7d60f266de8ba7915ebcf9fc9aa; uid_tt_ss=2803d7d60f266de8ba7915ebcf9fc9aa; sid_tt=865311383398fa9effd331fcb2312d74; sessionid=865311383398fa9effd331fcb2312d74; sessionid_ss=865311383398fa9effd331fcb2312d74; is_staff_user=false; sid_ucp_v1=1.0.0-KDExMWZmNzNmMTE1ZTBiMmM3ZDc2MWQ2MjMwZjc0ZWFmYjdhYzY1NzgKHwjTi-7FgwMQz_jvvgYY7zEgDDDKwLPcBTgCQPEHSAQaAmhsIiA4NjUzMTEzODMzOThmYTllZmZkMzMxZmNiMjMxMmQ3NA; ssid_ucp_v1=1.0.0-KDExMWZmNzNmMTE1ZTBiMmM3ZDc2MWQ2MjMwZjc0ZWFmYjdhYzY1NzgKHwjTi-7FgwMQz_jvvgYY7zEgDDDKwLPcBTgCQPEHSAQaAmhsIiA4NjUzMTEzODMzOThmYTllZmZkMzMxZmNiMjMxMmQ3NA; store-region=cn-bj; store-region-src=uid; login_time=1742470221435; stream_player_status_params=%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A0%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A0%7D%22; SelfTabRedDotControl=%5B%5D; _bd_ticket_crypt_cookie=19a45840a24e0abe695c240953140f61; __security_mc_1_s_sdk_sign_data_key_web_protect=f7a00302-4734-983b; __security_server_data_status=1; xg_device_score=7.454148831569444; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAAk-wgYh93rOJcHbqBmTkpWkwTe4Pr1imWHBM-Vh1Qu1w%2F1742486400000%2F0%2F0%2F1742470917103%22; FOLLOW_NUMBER_YELLOW_POINT_INFO=%22MS4wLjABAAAAk-wgYh93rOJcHbqBmTkpWkwTe4Pr1imWHBM-Vh1Qu1w%2F1742486400000%2F0%2F1742470317104%2F0%22; live_use_vvc=%22false%22; h265ErrorNum=-1; xgplayer_device_id=96436964381; FRIEND_NUMBER_RED_POINT_INFO=%22MS4wLjABAAAAk-wgYh93rOJcHbqBmTkpWkwTe4Pr1imWHBM-Vh1Qu1w%2F1742486400000%2F1742470718249%2F0%2F0%22; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1440%2C%5C%22screen_height%5C%22%3A960%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A8%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A7.05%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A100%7D%22; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCSmRoazVDR00yQm9iZVNDWndEVGZITk8vaUVPbmRFeFhyWStNY20yRDNubjJodk5QaHBkU0xhd05pMllqcHRzVTZFSUVMMW5tRmtQdTlYb00waGFROWs9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D; home_can_add_dy_2_desktop=%221%22; publish_badge_show_info=%220%2C0%2C0%2C1742470755418%22; odin_tt=cf4440d811ecb44f1785c571bc200902a4e09ee283b7996d9aff6a02282830b7cb2f10fade637977622f5ec8ac9a4def0d5001b9e11b0b0af29bdd312917b3fb; IsDouyinActive=true; passport_fe_beating_status=true; __ac_nonce=067dc1f6a00c5bc08db12")
    # 3. 获取需要搜索的关键字 yue9257
    arg = sys.argv[1:][0]
    userid = sys.argv[1:][1]
    # print(userid)
    # sys.exit()
    # 4. 获取video内容

    user_list = startUserSearch(arg)
    # print(user_list)
    for item in user_list['data']:
        # 添加链接到数据库
        # [0]['user_info']
        # 休眠一些时间
        sec_uid = item['user_info']['sec_uid']
        await saveUserLink("https://www.douyin.com/user/"+sec_uid,userid)
        # print()
    # 运行另外一个PY脚本
    subprocess.run("nohup bash /home/ubuntu/dy_start.sh  &", shell=True)


    result = startSearch(arg)
    for i in result['data']:
        print(i)
        if 'aweme_info' in i:
            video_id = i['aweme_info']['aweme_id']
            sleep(5)
            if video_id not in processed_videos:
                # pass
                tasks.append(getComments(video_id, userid))
                processed_videos.add(video_id)
    await asyncio.gather(*tasks)

    # 把编写好的代码传递到docker内部，然后重启docker
    # 把抓到的主播信息，保存到数据库里，然后启动搜索 改动代码最小了 也可以直接插入数据库，启动脚本就行了

    # 获取当前所需要的主播信息
    # 打开当前主播的信息
    # 抓取每一个主播下面的评论信息

    # 5.保存到数据库
    # http: // localhost: 8081 / douyin / search?keyword = xiaxiang

    # mu = MysqlUtils()

    # co = ChromiumOptions()

    # 如果是无界面系统，添加无头模式参数
    # co.set_argument('--headless=new')

    # 如果是Linux系统，尝试添加'--no-sandbox'启动参数
    # co.set_argument('--no-sandbox')
    # page = ChromiumPage(co)
    # mu.cur.execute("select * from pinglun_menu where is_search!=1")
    # page.listen.start("/aweme/v1/web/aweme/post/")
    # # return 1
    # processed_videos = set()  # 用于存储已经处理过的视频ID
    #
    #
    #
    # tasks = []
    # for row in mu.cur.fetchall():
    #     print(row)
    #     page.get(row[1].replace(" ", ""))
    #     sleep(0.2)
    #     sm = page.eles('xpath://*[@id="douyin-right-container"]/div[2]/div/div/div/div[3]/p[1]')
    #     # 判断是否是隐私账号
    #     if len(sm) != 0 or sm != []:
    #         continue
    #     try:
    #         res = page.listen.wait(timeout=10)
    #         #
    #         videos = res.response.body["aweme_list"]
    #         for video in videos:
    #             video_id = video['aweme_id']
    #             if video_id not in processed_videos:
    #                 print(video_id)
    #                 tasks.append(getComments(video_id, row[3]))
    #                 processed_videos.add(video_id)
    #         mu.cur.execute("update pinglun_menu set is_search=1 where url=%s", row[1])
    #         mu.con.commit()
    #     except Exception as e:
    #         print(e)
    #
    # await asyncio.gather(*tasks)
    # page.quit()


if __name__ == "__main__":
    asyncio.run(main())
